#!/bin/sh

MOD_DEVMEM=`ls /usr/fa*_devmem.ko`
  if [ "$MOD_DEVMEM" == "" ] ; then 
	echo "[Notice] Can't found devmem kernel module, some function won't work"
else
	loaded=`lsmod | grep devmem | awk '{print $1}'`
	if [ "$loaded" != "" ]; then rmmod $loaded; fi

        # Insert devmem with reserved buffer size 2MB
        insmod $MOD_DEVMEM devmem_reserveSize=0x200000
fi

if [ "`pwd`" == "/" ]; then
        PRJ_PATH=/mnt/nand1-1
	PRJ_DEVELOP_PATH=/mnt/nand1-2/   #User defined
else
	PRJ_PATH=`pwd`	# For anywhere
	PRJ_DEVELOP_PATH=`pwd`/DEVPATH/   #User defined
fi

PRJ_DEVELOP_PATH=/mnt/nand1-2	#User defined
cd $PRJ_PATH

export PATH="/mnt/ESensor/bin:/mnt/ESensor/sbin:$PRJ_PATH/wifi:$PATH"
export MSGTOTERMINAL=/dev/console
export LD_LIBRARY_PATH="/mnt/ESensor/lib:$LD_LIBRARY_PATH"

TS=`cat /proc/uptime | awk '{print $1}'`
echo -e "\033[1;33m[$TS] init done.\033[m"

#==============================================================================
#Framebuffer setting
#Never enter power saving on screen
if [ "$MSGTOTERMINAL" != "/dev/console" ] ; then
        echo 0 > /sys/class/graphics/fb0/blank
	stty -F $MSGTOTERMINAL cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
fi

#==============================================================================
# Upgrade
U_F_PATH="$PRJ_DEVELOP_PATH/ /mnt/sdcard/"

for i in $U_F_PATH; do
	if [ -f $i"ESensor_upgrade.zip" ]; then
	     cp -af $PRJ_PATH/upgrade.sh          /tmp
	     chmod +x /tmp/upgrade.sh
	     sync
	     /tmp/upgrade.sh $i
	fi
done
#==============================================================================

PRJ_NAME="ESensor"
PRJ_MP="/mnt/$PRJ_NAME"
# To mount SKYEYE ROMFS file
if [ -f $PRJ_PATH/$PRJ_NAME"_romfs.bin" ]; then
        if [ ! -d $PRJ_MP ]; then mkdir -p $PRJ_MP ; fi
        mount -t romfs $PRJ_PATH/$PRJ_NAME"_romfs.bin"          $PRJ_MP
        if [ ! "$?" == "0" ]; then
                echo "Mount "$PRJ_NAME"_romfs.bin failure."
	        # Execute SKYEYE on PRJ_DEVELOP_PATH
	        ln -s $PRJ_DEVELOP_PATH /mnt/ESensor
        else
		if [ -d $PRJ_PATH"/etc" ]; then
			ln -s $PRJ_PATH/etc		/tmp
		else
			echo "Configuration folder not exist." >  $MSGTOTERMINAL
			exit 0
		fi
	        df | grep /lib
	        ret=$?
	        if [ $ret = 0 ]; then umount /lib; fi  # Mounted
	fi
else
        echo $PRJ_PATH/$PRJ_NAME"_romfs.bin non-exist"
        echo "Run $PRJ_DEVELOP_PATH"
	# Execute SKYEYE on PRJ_DEVELOP_PATH
	ln -s $PRJ_DEVELOP_PATH		/mnt/ESensor  
        ln -s $PRJ_DEVELOP_PATH/etc	/tmp
fi

#==============================================================================
# Copy hostname to /etc
#==============================================================================
if [ -f /tmp/etc/hostname ]; then
	echo "echo hostname to kernel"
	cat /tmp/etc/hostname > /proc/sys/kernel/hostname
fi

#==============================================================================
if [ -f $PRJ_PATH"/lib_romfs.bin" ] && [ ! -f $PRJ_PATH/$PRJ_NAME"_romfs.bin" ]; then
	df | grep /lib
	ret=$?
	if [ $ret = 1 ]; then	# not mount
        	if [ ! -d "/lib" ]; then mkdir /lib; fi
       		mount -t romfs $PRJ_PATH"/lib_romfs.bin" /lib
	fi
else
	if [ ! -d "/lib" ]; then mkdir /lib; fi
	cp -a /mnt/ESensor/lib/ld-* /lib
fi

#==============================================================================
# setup network configuration
echo -e "\033[H\033[J" > $MSGTOTERMINAL
cd $PRJ_PATH
if ! ./setup_network.sh $PRJ_PATH; then
        echo "Configuration network setting fail!!" >  $MSGTOTERMINAL
fi

#================================================================================
# Run APP
# Configure GPIOD 0,1,2,3 as GPIO pins for LED control
#================================================================================
#nvtio -w 0xB0000098 0x20230000
# Execute SKYEYE
cd /mnt/ESensor
./go.sh &

#================================================================================
# UI subsystem
#================================================================================
echo "Run VM...."
if [ -d $PRJ_PATH"/gui" ]; then
	cd $PRJ_PATH/gui
	./go.sh &
elif [ -d $PRJ_DEVELOP_PATH"/gui" ]; then
        cd $PRJ_DEVELOP_PATH/gui
        ./go.sh &
fi
cd /mnt/ESensor
cd /mnt/nand1-2
sleep 2
/mnt/nand1-1/wap800/wap800_app &
cd /mnt/nand1-1
./monitoring.sh &
./netrelink.sh &

cd /mnt/nand1-1/wap800

./updateCheck.sh &

cd /mnt/nand1-2
